/* -------------------------------------- 

     Cherrypy webapp skeleton frontend

     License: LGPL
     Authors: saaj (mail@saaj.me)

   -------------------------------------- */

/*

#asset(qx/icon/${qx.icontheme}/22/actions/view-refresh.png)
#asset(qx/icon/${qx.icontheme}/22/actions/document-new.png)
#asset(qx/icon/${qx.icontheme}/22/actions/dialog-close.png)

*/

/**
 * Abstract table manager view class
 */
qx.Class.define("webapp.view.AbstractTable", {

	extend : qx.ui.container.Composite,
  type   : "abstract",
  
  construct : function()
  {
    this.base(arguments);

    this.set({
      layout        : new qx.ui.layout.VBox(),
      decorator     : "main",
      allowStretchX : false
    });

    this.add(this._createToolbar());
    
    var table = this._createTable();
    table.set({
      width  : 400,
      height : 300
    });
    this.add(table);
  },
  
  members : {
    
    _table   : null,
    _buttons : null,
    

    _createToolbar : function()
    {
      this._toolbar = new qx.ui.toolbar.ToolBar();
      this._buttons = {};
      
      var reload = new qx.ui.toolbar.Button(this.tr("Reload"), "icon/22/actions/view-refresh.png");
      reload.setMarginLeft(2);
      this.addButton("reload", reload);
      
      var create = new qx.ui.toolbar.Button(this.tr("Create"), "icon/22/actions/document-new.png");
      this.addButton("create", create);

      var remove = new qx.ui.toolbar.Button(this.tr("Remove"), "icon/22/actions/dialog-close.png");
      this.addButton("remove", remove);

      return this._toolbar;
    },
    
    addButton : function(name, button, position)
    {
      if(position != null)
      {
        this._toolbar.addAt(button, position);
      }
      else
      {
        this._toolbar.add(button);
      }

      this._buttons[name] = button;
    },

    getButtons : function()
    {
      return this._buttons;
    },
    
  	_createTable : function()
    {
      this._table = new qx.ui.table.Table();
      this._table.set({
        appearance                    : "widget",
        statusBarVisible              : false,
        columnVisibilityButtonVisible : false,
        showCellFocusIndicator        : false
      });
      
      return this._table;
    },

    _setColumns : function(options)
    {
    	var tableModel  = this._table.getTableModel();
      var columnModel = this._table.getTableColumnModel();

    	var titles = [];
      for(var name in options)
      {
        titles.push(options[name].title ? options[name].title : "");
      }

    	tableModel.setColumnIds(Object.keys(options));
      tableModel.setColumnNamesByIndex(titles);
      columnModel.init(tableModel.getColumnCount(), this._dataWidget);

      var index = 0;
      for(var name in options)
      {
        if(options[name].width)
        {
          columnModel.setColumnWidth(index, options[name].width);
        }
        else
        {
          columnModel.setColumnVisible(index, false);
        }

        tableModel.setColumnSortable(index, false);

        ++index;
      }
    },

    setTableModel : function(model)
    {
      this._table.setTableModel(model);
      
      this._setColumns();
    },

    getTable : function()
    {
      return this._table;
    }

  }

});